package br.com.minhasfinancas.api.model;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.ForeignKey;

import br.com.minhasfinancas.fw.model.AbstractModel;

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"titulo", "id_usuario"}, name = "UN_USUARIO_TITULO_USUARIO"))
@NamedQueries({
		@NamedQuery(name = "Conta.findAll", query = "SELECT c FROM Conta c"),
		@NamedQuery(name = "Conta.findById", query = "SELECT c FROM Conta c WHERE c.id = :id"),
		@NamedQuery(name = "Conta.findBySaldo", query = "SELECT c FROM Conta c WHERE c.saldo = :saldo"),
		@NamedQuery(name = "Conta.findByTitulo", query = "SELECT c FROM Conta c WHERE c.titulo = :titulo"),
		@NamedQuery(name = "Conta.findByUsuario", query = "SELECT c FROM Conta c WHERE c.usuario = :usuario"),
		@NamedQuery(name = "Conta.findByUsuarioTitulo", query = "SELECT c FROM Conta c WHERE c.usuario = :usuario AND c.titulo = :titulo"),
		@NamedQuery(name = "Conta.findByIdEager", query = "SELECT c FROM Conta c LEFT JOIN FETCH c.receitas LEFT JOIN FETCH c.despesas WHERE c.id = :id") })
public class Conta extends AbstractModel<Long> {
	private static final long serialVersionUID = 1L;

	public static final String FINDALL = "Conta.findAll";
	public static final String FINDBYID = "Conta.findByid";
	public static final String FINDBYSALDO = "Conta.findBySaldo";
	public static final String FINDBYTITULO = "Conta.findByTitulo";
	public static final String FINDBYUSUARIO = "Conta.findByUsuario";
	public static final String FINDBYUSUARIOTITULO = "Conta.findByUsuarioTitulo";
	public static final String FINDBYIDEAGER = "Conta.findByIdEager";

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID_CONTA")
	private Long id;

	@NotNull
	@Column(name = "TITULO", length = 45)
	private String titulo;

	@NotNull
	@Column(name = "SALDO")
	private double saldo;

	@OneToMany(mappedBy = "conta", fetch = FetchType.LAZY)
	private Set<Receita> receitas;

	@OneToMany(mappedBy = "conta", fetch = FetchType.LAZY)
	private Set<Despesa> despesas;

	@JoinColumn(name = "ID_USUARIO")
	@ManyToOne
	@ForeignKey(name = "FK_USUARIO_CONTA")
	private Usuario usuario;

	public Conta() {
	}

	@Override
	public Long getId() {
		return this.id;
	}

	@Override
	public void setId(Long id) {
		this.id = id;

	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public String getTitulo() {
		return titulo;
	}

	public void setTitulo(String titulo) {
		this.titulo = titulo;
	}

	public double getSaldo() {
		return saldo;
	}

	public void setSaldo(double saldo) {
		this.saldo = saldo;
	}

	public Set<Receita> getReceitas() {
		return receitas;
	}

	public void setReceitas(Set<Receita> receitas) {
		this.receitas = receitas;
	}

}
